package com.jwt;

import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class CreateJwt {

    public static void main(String[] args){
        // jwt加密无时间限制
        // create_time_nolimit();
        // jwt加密有过期时间
        create_time_imit();
    }

    // eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NjY2Iiwic3ViIjoiY2FybCIsImlhdCI6MTU0NjEwMTg2MX0.kZLksqqLgdwy5DMuyUPubjP4dWVLZWUsms8miEuhC5Y
    // eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NjY2Iiwic3ViIjoiY2FybCIsImlhdCI6MTU0NjEwMTg5NX0.jMtLeI0g7uC5S6QJHxtVBFNAm0oS4jk4WyjflOY0vLU
    protected static void create_time_nolimit() {
        JwtBuilder builder = Jwts.builder()
                .setId("6666")
                .setSubject("carl")
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256,"ten_square")
                ;

        System.out.println(" jwt : ");
        System.out.println(builder.compact());
    }

    // eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI3NzciLCJzdWIiOiJqYWNrIiwiaWF0IjoxNTQ2MTAyNTA5LCJleHAiOjE1NDYxMDI1Njl9.KqJErXKgtaoo19npsd91LtUvzKMrmeBdvs2p1S9_8Vg
    protected static void create_time_imit() {
        long expiration = System.currentTimeMillis() + 3000 * 60;
        JwtBuilder builder = Jwts.builder()
                .setId("777")
                .setSubject("jack")
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, "ten_square")
                .setExpiration(new Date(expiration))
                .claim("role","admin")
                ;

        System.out.println(" jwt : ");
        System.out.println(builder.compact());
    }


}
